const app=new Vue({
    el:"#div_id",
    data:{
        n:1,
        books: [
            {'name':'《算法导论》','date':'2006-9','price':85.00,'num':1},
            {'name':'《unix编程艺术》','date':'2006-2','price':59.00,'num':1},
            {'name':'《编程珠玑》','date':'2008-10','price':39.00,'num':1},
            {'name':'《代码大全》','date':'2006-3','price':128.00,'num':1}]
    },
    computed:{
        //只会执行一次，然后缓存值
    },
    methods:{
        dec:function (index) {
            if(index>this.books.length){
                return false;
            }
            this.books[index].num--;
        },
        add:function (index) {
            if(index>this.books.length){
                return false;
            }
            this.books[index].num++;
        },
        remove:function (index) {
            this.books.splice(index,1);
        },
        getTotalPrice:function () {
           /* let price=0;
         for(let book of this.books){
             let tp=book.price*book.num;
             price+=tp;
         }
         return this.showPrice(price);*/
       //利用reduce函数更简单,下面两种写法都可以
       /*let p= this.books.reduce(function (prev,curr) {
            return prev+curr.price*curr.num;
        },0);*/
       return this.books.reduce(((prev,curr)=>prev+curr.price*curr.num),0);

        },
        /**
         * 函数式写法
         * @param price
         * @returns {string}
         */
        showPrice:function (price) {
            return "￥" +price.toFixed(2);
        }
    },
    filters:{
        showPrice1:function (price) {
            return "￥" +price.toFixed(2);
        }
    }
});